package com.rzico.sbl.ui.report.dispatch

import android.view.View
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDispathchLookBinding
import com.rzico.sbl.other.serviceStep
import com.rzico.sbl.viewmodel.ReportDispatchViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import org.json.JSONArray
import org.json.JSONObject

class ReportDispatchLookActivity : BaseActivity(R.layout.activity_report_dispathch_look) {

    private val mBinding by lazy { ActivityReportDispathchLookBinding.bind(getRootView()) }

    override fun getViewModel() = get<ReportDispatchViewModel>()

    private lateinit var mOrderSn: String
    private lateinit var mShippingId: String
    private lateinit var mShippingSn: String

    override fun initData() {
        initTitle(title = "送达详情")

        getData()
    }

    private fun initTabLayout() {
        mBinding.apply {
            // 去除边缘阴影
            lookVp.getChildAt(0).overScrollMode = View.OVER_SCROLL_NEVER
            // 设置viewpager是否可以左右滑动，默认true
            lookVp.isUserInputEnabled = true

            // ViewPager2左右滑动时，默认不会预加载，Fragment生命周期
            // 未创建：onCreate → onStart → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            // 当点击tab时，会执行预加载，Fragment生命周期
            // 未创建时：预加载前一个(onCreate → onStart)
            //          → 当前Fragment(onCreate → onStart)
            //          → 预加载后一个(onCreate → onStart)
            //          → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            lookVp.adapter =
                object : FragmentStateAdapter(this@ReportDispatchLookActivity) {
                    override fun getItemCount() = serviceStep.intValue
                    override fun createFragment(position: Int) =
                        when (position) {
                            1 -> DispatchFirstFragment.newInstance(mShippingId)
                            2 -> DispatchSecondFragment.newInstance(mShippingSn)
                            else -> DispatchThirdFragment.newInstance(mShippingSn)
                        }
                }

            TabLayoutMediator(lookTab, lookVp) { tab, positon ->
                tab.text = when (positon) {
                    0 -> "送达"
                    1 -> "空桶"
                    2 -> "收款"
                    else -> ""
                }
            }.attach()
        }
    }

    private fun initRottenTabLayout() {
        mBinding.apply {
            // 去除边缘阴影
            lookVp.getChildAt(0).overScrollMode = View.OVER_SCROLL_NEVER
            // 设置viewpager是否可以左右滑动，默认true
            lookVp.isUserInputEnabled = true

            // ViewPager2左右滑动时，默认不会预加载，Fragment生命周期
            // 未创建：onCreate → onStart → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            // 当点击tab时，会执行预加载，Fragment生命周期
            // 未创建时：预加载前一个(onCreate → onStart)
            //          → 当前Fragment(onCreate → onStart)
            //          → 预加载后一个(onCreate → onStart)
            //          → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            lookVp.adapter =
                object : FragmentStateAdapter(this@ReportDispatchLookActivity) {
                    override fun getItemCount() = 3
                    override fun createFragment(position: Int) =
                        when (position) {
                            1 -> DispatchFirstFragment.newInstance(intent.getExtra("shipId"))
                            2 -> DispatchRottenFragment.newInstance(intent.getExtra("shippingSn"))
                            else -> DispatchThirdFragment.newInstance(intent.getExtra("shippingSn"))
                        }
                }

            TabLayoutMediator(lookTab, lookVp) { tab, positon ->
                tab.text = when (positon) {
                    0 -> "送达"
                    1 -> "空桶"
                    2 -> "烂桶"
                    else -> ""
                }
            }.attach()
        }
    }

    override fun initListener() {
        mOrderSn = intent.getExtra("orderSn")
        mShippingId = intent.getExtra("shipId")
        mShippingSn = intent.getExtra("shippingSn")
    }

    /**
     * 判断是否有烂桶商品
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .dispatchPay(mShippingSn)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = { result ->
                    val obj = JSONObject(result)

                    val hasRotten =
                        (obj.optJSONArray("shippingSwaps") ?: JSONArray()).length() > 0

                    if (hasRotten) initRottenTabLayout()
                    else initTabLayout()
                }
            )
    }

}